﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App
{
    public partial class frmorder : Form
    {
        string list = "";
        int n;
        List<int> changeList = new List<int>();
        List<T_OrdAssess> tOrder = null;
        private bool IsQuery = true;
        Entities db = new Entities();
        T_OrdAssess m_OrdAssess = null;
        T_OuterCtr m_OuterCtr = null;
        List<T_CtrSampleDetail> m_Ctrsample = null;
        private int id = 0;
        public int m_ordAssess 
        { 
            get{return id;}
            set { id = value; }
        }

        public List<int> oID
        {
            set
            {
                tOrder = new List<T_OrdAssess>();
                for (int i = 0; i < value.Count; i++)
                {
                    int id = value[i];
                    var list = db.T_OrdAssess.Where(a => a.oid == id);

                    tOrder.Add(list.FirstOrDefault());
                }
            }
        }
        private int ordAssess
        {
            set
            {
                var list = db.T_OrdAssess.Where(a => a.oid == value);
                foreach (var ordassesslist in list)
                {
                    m_OrdAssess = ordassesslist;
                    break;
                }
                if (m_OrdAssess == null) return;
                txtcontractNo.Text = m_OrdAssess.contractNo;
                txtbrand.Text = m_OrdAssess.brand;
                txtyName.Text = m_OrdAssess.yName;
                txtfinalCustom.Text = m_OrdAssess.finalCustom;
                dtcustDate.ValueText = m_OrdAssess.custDate.ToString(); 
                txtfactory.Text = m_OrdAssess.factory;
                dtfactoryDate.ValueText = m_OrdAssess.factoryDate.ToString(); 
                txtorderNo.Text = m_OrdAssess.orderNo.ToString();
                txtdFactory.Text = m_OrdAssess.dFactory;
                txtoutsoleNo.Text = m_OrdAssess.outsoleNo.ToString();
                txtoutsoleNowCode.Text = m_OrdAssess.outsoleNowCode;
                txtoutsoleColourMatch.Text = m_OrdAssess.outsoleColourMatch;
                txtcompModuleNo.Text = m_OrdAssess.compModuleNo.ToString();
                txtlastNo.Text = m_OrdAssess.lastNo.ToString();
                txtlastCodeSegment.Text = m_OrdAssess.lastCodeSegment;
                txtbefFactory.Text = m_OrdAssess.befFactory;
                txtupperMaterial.Text = m_OrdAssess.upperMaterial;
                txtoutsoleMaterial.Text = m_OrdAssess.outsoleMaterial;
                txtappSampleOpinion.Text = m_OrdAssess.appSampleOpinion;
                txtPreparedDocument.Text = m_OrdAssess.PreparedDocument;
                txtzdYname.Text = m_OrdAssess.yName;
                txtymanager.Text = m_OrdAssess.ymanager;
                dtPreparedDocumentDate.ValueText = m_OrdAssess.PreparedDocumentDate.ToString();
                dtYcheckDate.ValueText = m_OrdAssess.YcheckDate.ToString();
                dtYmanacheckDate.ValueText = m_OrdAssess.YmanacheckDate.ToString();

                cbstate.Text = "未生成";

                if (m_OrdAssess.isRepeatOrder == "是") rbisRepeatOrder1.Checked = true;
                //else if (m_OrdAssess.isRepeatOrder == "否") rbisRepeatOrder2.Checked = true;
                if (m_OrdAssess.isSM == "是") rbisSM1.Checked = true;
                //else if (m_OrdAssess.isSM == "否") rbisSM2.Checked = true;
                if (m_OrdAssess.isAddMolds == "是") rbisAddMolds1.Checked = true;
                //else if (m_OrdAssess.isAddMolds == "否") rbisAddMolds2.Checked = true;
                if (m_OrdAssess.isbrand == "是") rbisbrand1.Checked = true;
                //ese if (m_OrdAssess.isbrand == "否") rbisbrand2.Checked = true;
                if (m_OrdAssess.isNobrand == "是") rbisNobrand1.Checked = true;
                //else if (m_OrdAssess.isNobrand == "否") rbisNobrand2.Checked = true;
                if (m_OrdAssess.isproof == "是") rbisproof1.Checked = true;
                //else if (m_OrdAssess.isproof == "否") rbisproof2.Checked = true;
                if (m_OrdAssess.ispackage == "是") rbispackage1.Checked = true;
                //else if (m_OrdAssess.ispackage == "否") rbispackage2.Checked = true;
                if (m_OrdAssess.isYd == "是") rbisYd1.Checked = true;
                //else if (m_OrdAssess.isYd == "否") rbisYd2.Checked = true;
                if (m_OrdAssess.ischangeprice == "是")
                {
                    rbischangeprice1.Checked = true;
                    txtbeforeprice.Text = m_OrdAssess.beforeprice;
                    txtnowprice.Text = m_OrdAssess.nowprice;
                    txtchangecause.Text = m_OrdAssess.changecause;
                }
                //else if (m_OrdAssess.ischangeprice == "否") rbischangeprice2.Checked = true;
                if (m_OrdAssess.isNewStyle == "是") rbisNewStyle1.Checked = true;
                //else if (m_OrdAssess.isNewStyle == "否") rbisNewStyle2.Checked = true;
                if (m_OrdAssess.isbefShipment == "是") rbisbefShipment1.Checked = true;
                //else if (m_OrdAssess.isbefShipment == "否") rbisbefShipment2.Checked = true;
                if (m_OrdAssess.istestSample == "是") rbistestSample1.Checked = true;
                //else if (m_OrdAssess.istestSample == "否") rbistestSample2.Checked = true;
                if (m_OrdAssess.isSendSj == "是")
                {
                    rbisSendSj1.Checked = true;
                    if (m_OrdAssess.antenatal == "1") chbantenatal.Checked = true;
                    if (m_OrdAssess.basecode == "1") chbbasecode.Checked = true;
                }
                //else if (m_OrdAssess.isSendSj == "否") rbisSendSj2.Checked = true;
                if (m_OrdAssess.orderProduct == "1") chborderProduct.Checked = true;
                if (m_OrdAssess.dc == "1") cbhdc.Checked = true;
                if (m_OrdAssess.appSampleState[0] == 1) chbappSampleState.SetItemChecked(0, true);
                if (m_OrdAssess.appSampleState[1] == 1) chbappSampleState.SetItemChecked(1, true);
                if (m_OrdAssess.appSampleState[2] == 1) chbappSampleState.SetItemChecked(2, true);
                if (m_OrdAssess.appSampleState[3] == 1) chbappSampleState.SetItemChecked(3, true);

                var detaillist = db.T_CtrSampleDetail.Where(a => a.oid == value).ToList();
                m_Ctrsample = detaillist;
                bs1.DataSource = detaillist;
                gv2.DataSource = bs1;
                bnav1.BindingSource = bs1;

            }
        }
        public frmorder()
        {
            InitializeComponent();
            panList.BringToFront();
            gvDataSource();
        }
        private void gvDataSource()
        {
            var list = db.T_OrdAssess.Where(a => a.state.Trim() == "未生成").ToList();
            bslist.DataSource = list;
            bnavList.BindingSource = bslist;
        }
        private void gvID()
        {
            try
            {
                if (gvlist.RowCount > 0)
                {
                    int index = gvlist.CurrentRow.Index;
                    m_ordAssess = int.Parse(gvlist.Rows[index].Cells["coloID"].Value.ToString());
                    ordAssess = int.Parse( gvlist.Rows[index].Cells["coloID"].Value.ToString());
                }
                else
                {
                    TxtClear();
                }
            }
            catch (Exception exp)
            {

                Msg.ShowError(exp.Message.ToString() + exp.Source.ToString());
            }
        }
        private void SaveAdd()
        {
            m_OrdAssess = new T_OrdAssess();
            if (db.T_OrdAssess.Count() <= 0) m_OrdAssess.oid = 1;
            else m_OrdAssess.oid = db.T_OrdAssess.OrderByDescending(a=>a.oid).First().oid + 1;
            m_OrdAssess.contractNo = txtcontractNo.Text;
            m_OrdAssess.brand = txtbrand.Text;
            m_OrdAssess.yName = txtyName.Text;
            m_OrdAssess.finalCustom = txtfinalCustom.Text;
            if (!string.IsNullOrEmpty(dtcustDate.ValueText)) m_OrdAssess.custDate = Convert.ToDateTime(dtcustDate.Value);
            m_OrdAssess.factory = txtfactory.Text;
            if (!string.IsNullOrEmpty(dtfactoryDate.ValueText)) m_OrdAssess.factoryDate = Convert.ToDateTime(dtfactoryDate.Value);
            if (!string.IsNullOrEmpty(txtorderNo.Text)) m_OrdAssess.orderNo = int.Parse(txtorderNo.Text);
            m_OrdAssess.dFactory = txtdFactory.Text;
            if (!string.IsNullOrEmpty(txtoutsoleNo.Text)) m_OrdAssess.outsoleNo = int.Parse(txtoutsoleNo.Text);
            m_OrdAssess.outsoleNowCode = txtoutsoleNowCode.Text;
            m_OrdAssess.outsoleColourMatch = txtoutsoleColourMatch.Text;
            if (!string.IsNullOrEmpty(txtcompModuleNo.Text)) m_OrdAssess.compModuleNo = int.Parse(txtcompModuleNo.Text);
            if (!string.IsNullOrEmpty(txtlastNo.Text)) m_OrdAssess.lastNo = int.Parse(txtlastNo.Text);
            m_OrdAssess.lastCodeSegment = txtlastCodeSegment.Text;
            m_OrdAssess.befFactory = txtbefFactory.Text;
            m_OrdAssess.upperMaterial = txtupperMaterial.Text;
            m_OrdAssess.outsoleMaterial = txtoutsoleMaterial.Text;
            m_OrdAssess.appSampleOpinion = txtappSampleOpinion.Text;
            m_OrdAssess.PreparedDocument = txtPreparedDocument.Text;
            m_OrdAssess.yName = txtzdYname.Text;
            m_OrdAssess.ymanager = txtymanager.Text;
            if(!string.IsNullOrEmpty(dtPreparedDocumentDate.ValueText)) m_OrdAssess.PreparedDocumentDate = Convert.ToDateTime(dtPreparedDocumentDate.Value);
            if (!string.IsNullOrEmpty(dtYcheckDate.ValueText)) m_OrdAssess.YcheckDate = Convert.ToDateTime(dtYcheckDate.Value);
            if (!string.IsNullOrEmpty(dtYmanacheckDate.ValueText)) m_OrdAssess.YmanacheckDate = Convert.ToDateTime(dtYmanacheckDate.Value);

            m_OrdAssess.state = "未生成";
            
            if (rbisRepeatOrder1.Checked)
            {
                m_OrdAssess.isRepeatOrder = "是";
            }
            else if (rbisRepeatOrder2.Checked) m_OrdAssess.isRepeatOrder = "否";
            if (rbisSM1.Checked)
            {
                m_OrdAssess.isSM = "是";
            }
            else if (rbisSM2.Checked) m_OrdAssess.isSM = "否";
            if (rbisAddMolds1.Checked)
            {
                m_OrdAssess.isAddMolds = "是";
            }
            else if (rbisAddMolds2.Checked) m_OrdAssess.isAddMolds = "否";

            if (rbisbrand1.Checked)
            {
                m_OrdAssess.isbrand = "是";
            }
            else if (rbisbrand2.Checked) m_OrdAssess.isbrand = "否";
            if (rbisNobrand1.Checked)
            {
                m_OrdAssess.isNobrand = "是";
            }
            else if (rbisNobrand2.Checked) m_OrdAssess.isNobrand = "否";
            if (rbisproof1.Checked)
            {
                m_OrdAssess.isproof = "是";
            }
            else if (rbisproof2.Checked) m_OrdAssess.isproof = "否";
            if (rbispackage1.Checked)
            {
                m_OrdAssess.ispackage = "是";
            }
            else if (rbispackage2.Checked) m_OrdAssess.ispackage = "否";
            if (rbisYd1.Checked)
            {
                m_OrdAssess.isYd = "是";
            }
            else if (rbisYd2.Checked) m_OrdAssess.isYd = "否";
            if (rbisNewStyle1.Checked)
            {
                m_OrdAssess.isNewStyle = "是";
            }
            else if (rbisNewStyle2.Checked) m_OrdAssess.isNewStyle = "否";
            if (rbisbefShipment1.Checked)
            {
                m_OrdAssess.isbefShipment = "是";
            }
            else if (rbisbefShipment2.Checked) m_OrdAssess.isbefShipment = "否";
            if (rbistestSample1.Checked)
            {
                m_OrdAssess.istestSample = "是";
            }
            else if (rbistestSample2.Checked) m_OrdAssess.istestSample = "否";
            if (rbischangeprice1.Checked)
            {
                m_OrdAssess.istestSample = "是";
                m_OrdAssess.beforeprice = txtbeforeprice.Text;
                m_OrdAssess.nowprice = txtnowprice.Text;
                m_OrdAssess.changecause = txtchangecause.Text;
            }
            else if (rbischangeprice2.Checked) m_OrdAssess.istestSample = "否";
            if (rbisSendSj1.Checked)
            {
                m_OrdAssess.isSendSj = "是";
                if (chbantenatal.Checked) m_OrdAssess.antenatal = "1";
                else m_OrdAssess.antenatal = "0";
                if (chbbasecode.Checked) m_OrdAssess.basecode = "1";
                else m_OrdAssess.basecode = "0";
            }
            else if (rbisSendSj2.Checked) m_OrdAssess.isSendSj = "否";
            if (chborderProduct.Checked) m_OrdAssess.orderProduct = "1";
            else m_OrdAssess.orderProduct = "0";
            if (cbhdc.Checked) m_OrdAssess.dc = "1";
            else m_OrdAssess.dc = "0";
            for (n = 0; n < chblaststate.Items.Count; n++)
            {
                if (chblaststate.GetItemChecked(n))
                {
                    list += "1";
                }
                else list += "0";
            }
            m_OrdAssess.laststate = list.ToString();
            for (n = 0; n < chbappSampleState.Items.Count; n++)
            {
                if (chbappSampleState.GetItemChecked(n))
                {
                    list += "1";
                }
                else list += "0";
            }
            m_OrdAssess.appSampleState = list.ToString();
            db.T_OrdAssess.Add(m_OrdAssess);
            int i = db.SaveChanges();

            for (int a = 0; a < gv2.RowCount-1; a++)
            {
                T_CtrSampleDetail m_CtrSam = new T_CtrSampleDetail();
                if(db.T_CtrSampleDetail.Count()<1) m_CtrSam.csID = 1;
                else m_CtrSam.csID = db.T_CtrSampleDetail.OrderByDescending(b => b.csID).First().csID + 1;
                m_CtrSam.oid = m_OrdAssess.oid;
                if (gv2.Rows[a].Cells[0].Value != null) m_CtrSam.productNo = gv2.Rows[a].Cells[0].Value.ToString();
                if (gv2.Rows[a].Cells[1].Value != null) m_CtrSam.yard = gv2.Rows[a].Cells[1].Value.ToString();
                if (gv2.Rows[a].Cells[2].Value != null) m_CtrSam.sCount = int.Parse(gv2.Rows[a].Cells[2].Value.ToString());
                if (gv2.Rows[a].Cells[3].Value != null) m_CtrSam.fobPrice = decimal.Parse(gv2.Rows[a].Cells[3].Value.ToString());
                if (gv2.Rows[a].Cells[4].Value != null) m_CtrSam.factPrice = decimal.Parse(gv2.Rows[a].Cells[4].Value.ToString());
                if (gv2.Rows[a].Cells[5].Value != null) m_CtrSam.swap = decimal.Parse(gv2.Rows[a].Cells[5].Value.ToString());
                if (gv2.Rows[a].Cells[6].Value != null) m_CtrSam.SMbillPrice = decimal.Parse(gv2.Rows[a].Cells[6].Value.ToString());

                db.T_CtrSampleDetail.Add(m_CtrSam);
                db.SaveChanges();
            }

            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }
        private void SaveModify()
        {
            if (m_OrdAssess == null) return;

            m_OrdAssess.contractNo = txtcontractNo.Text;
            m_OrdAssess.brand = txtbrand.Text;
            m_OrdAssess.yName = txtyName.Text;
            m_OrdAssess.finalCustom = txtfinalCustom.Text;
            if (!string.IsNullOrEmpty(dtcustDate.ValueText)) m_OrdAssess.custDate = Convert.ToDateTime(dtcustDate.Value);
            m_OrdAssess.factory = txtfactory.Text;
            if (!string.IsNullOrEmpty(dtfactoryDate.ValueText)) m_OrdAssess.factoryDate = Convert.ToDateTime(dtfactoryDate.Value);
            if (!string.IsNullOrEmpty(txtorderNo.Text)) m_OrdAssess.orderNo = int.Parse(txtorderNo.Text);
            m_OrdAssess.dFactory = txtdFactory.Text;
            if (!string.IsNullOrEmpty(txtoutsoleNo.Text)) m_OrdAssess.outsoleNo = int.Parse(txtoutsoleNo.Text);
            m_OrdAssess.outsoleNowCode = txtoutsoleNowCode.Text;
            m_OrdAssess.outsoleColourMatch = txtoutsoleColourMatch.Text;
            if (!string.IsNullOrEmpty(txtcompModuleNo.Text)) m_OrdAssess.compModuleNo = int.Parse(txtcompModuleNo.Text);
            if (!string.IsNullOrEmpty(txtlastNo.Text)) m_OrdAssess.lastNo = int.Parse(txtlastNo.Text);
            m_OrdAssess.lastCodeSegment = txtlastCodeSegment.Text;
            m_OrdAssess.befFactory = txtbefFactory.Text;
            m_OrdAssess.upperMaterial = txtupperMaterial.Text;
            m_OrdAssess.outsoleMaterial = txtoutsoleMaterial.Text;
            m_OrdAssess.appSampleOpinion = txtappSampleOpinion.Text;
            m_OrdAssess.PreparedDocument = txtPreparedDocument.Text;
            m_OrdAssess.yName = txtzdYname.Text;
            m_OrdAssess.ymanager = txtymanager.Text;
            if (!string.IsNullOrEmpty(dtPreparedDocumentDate.ValueText)) m_OrdAssess.PreparedDocumentDate = Convert.ToDateTime(dtPreparedDocumentDate.Value);
            if (!string.IsNullOrEmpty(dtYcheckDate.ValueText)) m_OrdAssess.YcheckDate = Convert.ToDateTime(dtYcheckDate.Value);
            if (!string.IsNullOrEmpty(dtYmanacheckDate.ValueText)) m_OrdAssess.YmanacheckDate = Convert.ToDateTime(dtYmanacheckDate.Value);

            m_OrdAssess.state = "未生成";

            if (rbisRepeatOrder1.Checked)
            {
                m_OrdAssess.isRepeatOrder = "是";
            }
            else if (rbisRepeatOrder2.Checked) m_OrdAssess.isRepeatOrder = "否";
            if (rbisSM1.Checked)
            {
                m_OrdAssess.isSM = "是";
            }
            else if (rbisSM2.Checked) m_OrdAssess.isSM = "否";
            if (rbisAddMolds1.Checked)
            {
                m_OrdAssess.isAddMolds = "是";
            }
            else if (rbisAddMolds2.Checked) m_OrdAssess.isAddMolds = "否";

            if (rbisbrand1.Checked)
            {
                m_OrdAssess.isbrand = "是";
            }
            else if (rbisbrand2.Checked) m_OrdAssess.isbrand = "否";
            if (rbisNobrand1.Checked)
            {
                m_OrdAssess.isNobrand = "是";
            }
            else if (rbisNobrand2.Checked) m_OrdAssess.isNobrand = "否";
            if (rbisproof1.Checked)
            {
                m_OrdAssess.isproof = "是";
            }
            else if (rbisproof2.Checked) m_OrdAssess.isproof = "否";
            if (rbispackage1.Checked)
            {
                m_OrdAssess.ispackage = "是";
            }
            else if (rbispackage2.Checked) m_OrdAssess.ispackage = "否";
            if (rbisYd1.Checked)
            {
                m_OrdAssess.isYd = "是";
            }
            else if (rbisYd2.Checked) m_OrdAssess.isYd = "否";
            if (rbisNewStyle1.Checked)
            {
                m_OrdAssess.isNewStyle = "是";
            }
            else if (rbisNewStyle2.Checked) m_OrdAssess.isNewStyle = "否";
            if (rbisbefShipment1.Checked)
            {
                m_OrdAssess.isbefShipment = "是";
            }
            else if (rbisbefShipment2.Checked) m_OrdAssess.isbefShipment = "否";
            if (rbistestSample1.Checked)
            {
                m_OrdAssess.istestSample = "是";
            }
            else if (rbistestSample2.Checked) m_OrdAssess.istestSample = "否";
            if (rbischangeprice1.Checked)
            {
                m_OrdAssess.istestSample = "是";
                m_OrdAssess.beforeprice = txtbeforeprice.Text;
                m_OrdAssess.nowprice = txtnowprice.Text;
                m_OrdAssess.changecause = txtchangecause.Text;
            }
            else if (rbischangeprice2.Checked) m_OrdAssess.istestSample = "否";
            if (rbisSendSj1.Checked)
            {
                m_OrdAssess.isSendSj = "是";
                if (chbantenatal.Checked) m_OrdAssess.antenatal = "1";
                else m_OrdAssess.antenatal = "0";
                if (chbbasecode.Checked) m_OrdAssess.basecode = "1";
                else m_OrdAssess.basecode = "0";
            }
            else if (rbisSendSj2.Checked) m_OrdAssess.isSendSj = "否";
            if (chborderProduct.Checked) m_OrdAssess.orderProduct = "1";
            else m_OrdAssess.orderProduct = "0";
            if (cbhdc.Checked) m_OrdAssess.dc = "1";
            else m_OrdAssess.dc = "0";
            for (n = 0; n < chblaststate.Items.Count; n++)
            {
                if (chblaststate.GetItemChecked(n))
                {
                    list += "1";
                }
                else list += "0";
            }
            m_OrdAssess.laststate = list.ToString();
            for (n = 0; n < chbappSampleState.Items.Count; n++)
            {
                if (chbappSampleState.GetItemChecked(n))
                {
                    list += "1";
                }
                else list += "0";
            }
            m_OrdAssess.appSampleState = list.ToString();

            int i = db.SaveChanges();
 
            Msg.ShowInfo(string.Format("已保存{0}条订单信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void rbischangeprice_click(object sender, EventArgs e)
        {
            if (rbischangeprice1.Checked)
            {
                txtbeforeprice.Enabled = true;
                txtnowprice.Enabled = true;
                txtchangecause.Enabled = true;

            }
            else if (rbischangeprice2.Checked)
            {
                txtbeforeprice.Enabled = false;
                txtnowprice.Enabled = false;
                txtchangecause.Enabled = false;
                Helper.TxtClear(txtbeforeprice);
                Helper.TxtClear(txtnowprice);
                Helper.TxtClear(txtchangecause);
            }
        }
        private void TxtClear()
        {
            Helper.TxtClear(txtcontractNo);
            Helper.TxtClear(txtbrand);
            Helper.TxtClear(txtyName);
            Helper.TxtClear(txtfinalCustom);
            dtcustDate.ValueText = "";
            Helper.TxtClear(txtfactory);
            dtfactoryDate.ValueText = ""; 
            Helper.TxtClear(txtorderNo);
            Helper.TxtClear(txtdFactory);
            Helper.TxtClear(txtoutsoleNo);
            Helper.TxtClear(txtoutsoleNowCode);
            Helper.TxtClear(txtoutsoleColourMatch);
            Helper.TxtClear(txtcompModuleNo);
            Helper.TxtClear(txtlastNo);
            Helper.TxtClear(txtlastCodeSegment);
            Helper.TxtClear(txtbefFactory);
            Helper.TxtClear(txtupperMaterial);
            Helper.TxtClear(txtoutsoleMaterial);
            Helper.TxtClear(txtappSampleOpinion);
            Helper.TxtClear(txtPreparedDocument);
            Helper.TxtClear(txtzdYname);
            Helper.TxtClear(txtymanager);
            dtPreparedDocumentDate.ValueText = "";
            dtYcheckDate.ValueText = "";
            dtYmanacheckDate.ValueText = "";
            if (chborderProduct.Checked) chborderProduct.Checked = false;
            if (cbhdc.Checked) cbhdc.Checked = false;
            rbisAddMolds1.Checked = true;
            rbisRepeatOrder1.Checked = true;
            rbisSM1.Checked = true;
            rbisbrand1.Checked = true;
            rbisNobrand1.Checked = true;
            rbisproof1.Checked = true;
            rbispackage1.Checked = true;
            rbisYd1.Checked = true;
            rbisbefShipment1.Checked = true;
            rbisNewStyle1.Checked = true;
            rbisSendSj1.Checked = true;
            rbistestSample1.Checked = true;
            rborderType1.Checked = true;
            rbischangeprice1.Checked = true;
            for (int n = 0; n < chblaststate.Items.Count; n++) 
            {
                chblaststate.SetItemChecked(n, false); 
            }
            for (int n = 0; n < chbappSampleState.Items.Count; n++)
            {
                chbappSampleState.SetItemChecked(n, false);
            }
            if(rbisSendSj1.Checked)
            {
                chbantenatal.Checked = false;
                chbbasecode.Checked = false;
            }
            gv2.Rows.Clear();
        }

        private void rbisneed_click(object sender, EventArgs e)
        {
            if (rbisSendSj1.Checked)
            {
                chbantenatal.Enabled = true;
                chbbasecode.Enabled = true;
            }
            else if (rbisSendSj2.Checked)
            {
                chbantenatal.Enabled = false;
                chbbasecode.Enabled = false;
                chbantenatal.Checked = false;
                chbbasecode.Checked = false;
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            gvID();
            if (IsQuery)
            {
                this.panList.BringToFront();
                btn1.Text = "明细";
                this.IsQuery = false;
            }
            else
            {
                this.panDetail.BringToFront();
                btn1.Text = "概要";
                this.IsQuery = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            m_OrdAssess = null;
            this.panDetail.BringToFront();
            btn1.Text = "概要";
            this.IsQuery = false;
            TxtClear();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btn1_Click(null, null);
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，您确定要取消吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
                
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信删除此信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                gvID();
                if (m_ordAssess == 0)
                {
                    Msg.ShowError("请选择一条记录！");
                    return;
                }
                var outordAssess = db.T_OrdAssess.First(a => a.oid == m_ordAssess);
                db.T_OrdAssess.Remove(outordAssess);
                int i = db.SaveChanges();
                Msg.ShowInfo(string.Format("已删除{0}条订单信息。", i));
                gvDataSource();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信保存订单信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (m_OrdAssess == null)
                {
                    if (db.T_OrdAssess.Where(a => a.contractNo == txtcontractNo.Text).Count() > 0)
                    {
                        Msg.ShowWarning("已存在的订单号，请重新输入！");
                        return;
                    }
                    SaveAdd();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                    
                }
                else
                {
                    SaveModify();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                    
                }
            }


        }

        
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            
            if (changeList != null) 
            {
                this.oID = changeList;
                int i = 0;
                foreach(var oidlist in tOrder)
                {
                    m_OrdAssess = oidlist;
                    if (m_OrdAssess.state.Trim() == "已生成")
                    {
                        Msg.ShowWarning("包含已生成订单，请重新选择！");
                        return;
                    }
                    m_OuterCtr = new T_OuterCtr();
                    m_OuterCtr.oID = db.T_OuterCtr.OrderByDescending(a => a.oID).First().oID + 1; 
                    m_OuterCtr.finalCustom = m_OrdAssess.finalCustom;
                    m_OuterCtr.yName = m_OrdAssess.yName;
                    m_OuterCtr.oCtrNo = m_OrdAssess.contractNo;
                    m_OuterCtr.shipDate1 = m_OrdAssess.custDate;
                    m_OuterCtr.shipDate2 = m_OrdAssess.custDate;
                    m_OuterCtr.state = "未生成";
                    //m_OuterCtr.fob = m_OrdAssess.fob;
                    //m_OuterCtr.soleColor = m_OrdAssess.outsoleColourMatch;
                    //m_OuterCtr.sCount=m_OrdAssess.number;
                    //m_OuterCtr.brand=m_OrdAssess.brand;
                    //m_OuterCtr.sSize=m_OrdAssess.codeSegment;
                    //m_OuterCtr.SoleFactory=m_OrdAssess.dFactory;
                    //m_OuterCtr.SiMo=m_OrdAssess.isSM;
                    //m_OuterCtr.SoleNo=m_OrdAssess.outsoleNo;
                    //m_OuterCtr.xtNo=m_OrdAssess.lastNo;
                    //m_OuterCtr.xtNo=m_OrdAssess.lastNo;
                    db.T_OuterCtr.Add(m_OuterCtr);
                    m_OrdAssess.state = "已生成";
                    db.SaveChanges();
                    i++;

                    var list = db.T_CtrSampleDetail.Where(a => a.oid == m_OrdAssess.oid);
                    foreach (var ctr in list)
                    {
                        ctr.xID = m_OuterCtr.oID;
                        db.SaveChanges();
                    }
                    changeList.Remove(m_OrdAssess.oid);
                }
                
                Msg.ShowInfo(string.Format("已生成{0}条外合同信息。",i));
                gvDataSource();
            }
            else
            {
                Msg.ShowError("请选择表中信息!");
                return;
            }
        }

        private void gvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex==0&&e.RowIndex!=-1)
            {
                if (bool.Parse(this.gvlist.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == true)
                {
                    changeList.Add(int.Parse(gvlist.Rows[e.RowIndex].Cells["coloID"].Value.ToString()));
                }
                else if (bool.Parse(this.gvlist.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()) == false)
                {
                    changeList.Remove(int.Parse(gvlist.Rows[e.RowIndex].Cells["coloID"].Value.ToString()));
                }
            }
        }

        private void gvlist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.ordAssess = int.Parse(gvlist.Rows[e.RowIndex].Cells["coloID"].Value.ToString());
                this.panDetail.BringToFront();
                btn1.Text = "概要";
                this.IsQuery = false;
            }
            catch (Exception)
            {

                Msg.ShowError("请选择表里的数据！");
            }
        }

        private void frmorder_Load(object sender, EventArgs e)
        {
            cbstate.SelectedIndex = 1;
        }

    }
}
